<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="人名" prop="orgPeopleName">
        <el-input
          v-model="queryParams.orgPeopleName"
          placeholder="请输入人名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="介绍" prop="orgPeopleIntroduce">
        <el-input
          v-model="queryParams.orgPeopleIntroduce"
          placeholder="请输入介绍"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属商户/协会" prop="vendorId">
        <el-input
          v-model="queryParams.vendorId"
          placeholder="请输入归属商户/协会"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入归属店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['api:memberOrganizational:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['api:memberOrganizational:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:memberOrganizational:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['api:memberOrganizational:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberOrganizationalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="组织id" align="center" prop="organizationalId" />
      <el-table-column label="职位" align="center" prop="organizationalType" />
      <el-table-column label="人名" align="center" prop="orgPeopleName" />
      <el-table-column label="头像" align="center" prop="orgPeopleImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.orgPeopleImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="介绍" align="center" prop="orgPeopleIntroduce" />
<!--      <el-table-column label="归属商户/协会" align="center" prop="vendorId" />-->
<!--      <el-table-column label="2-协会，100-商户" align="center" prop="vendorType" />-->
      <el-table-column label="归属店铺名称" align="center" prop="storeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:memberOrganizational:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:memberOrganizational:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改组织架构对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="orgPeopleName">
          <el-input v-model="form.orgPeopleName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="职位" prop="organizationalType">
          <el-input v-model="form.organizationalType" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="头像" prop="orgPeopleImage">
          <image-upload v-model="form.orgPeopleImage"/>
        </el-form-item>
        <el-form-item label="介绍" prop="orgPeopleIntroduce">
          <el-input   type="textarea" v-model="form.orgPeopleIntroduce" placeholder="请输入介绍" />
        </el-form-item>
        <el-form-item label="所属单位" prop="vendorId">
          <el-select  @change="changeStore" v-model="form.vendorId" placeholder="请选择">
            <el-option
              v-for="item in storelist"
              :key="item.value"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="归属店铺名称" prop="storeName">-->
<!--          <el-input v-model="form.storeName" placeholder="请输入归属店铺名称" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMemberOrganizational, getMemberOrganizational, delMemberOrganizational, addMemberOrganizational, updateMemberOrganizational } from "@/api/module/memberOrganizational";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus, deptTreeSelect,changeListUser } from "@/api/system/user";
export default {
  name: "MemberOrganizational",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 组织架构表格数据
      memberOrganizationalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        organizationalType: null,
        orgPeopleName: null,
        orgPeopleImage: null,
        orgPeopleIntroduce: null,
        vendorId: null,
        vendorType: null,
        storeName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    //  商户表
      storelist:[]
    };
  },
  created() {
    this.getstorelist()
    this.getList();
  },
  methods: {
    //获取商户列表
    getstorelist(){
      changeListUser({pageNum: 1, pageSize: 100,}).then(res=>{
        this.storelist=res.rows
      })
    },
    /** 查询组织架构列表 */
    getList() {
      this.loading = true;
      listMemberOrganizational(this.queryParams).then(response => {
        this.memberOrganizationalList = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },

    //选中
    changeStore(e){
      this.form.vendorId=e
        this.storelist.forEach((item,index)=>{
          if(item.userId==e){
            this.form.storeName=item.nickName
          }
        })

    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        organizationalId: null,
        organizationalType: null,
        orgPeopleName: null,
        orgPeopleImage: null,
        orgPeopleIntroduce: null,
        vendorId: null,
        vendorType: null,
        storeName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.organizationalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加组织架构";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const organizationalId = row.organizationalId || this.ids
      getMemberOrganizational(organizationalId).then(response => {
        this.form = response.data;

        this.storelist.forEach(item=>{
          if(this.form.vendorId==item.userId){
            this.form.vendorId=item.nickName
          }
        })
        this.open = true;
        this.title = "修改组织架构";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.organizationalId != null) {
            updateMemberOrganizational(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMemberOrganizational(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const organizationalIds = row.organizationalId || this.ids;
      this.$modal.confirm('是否确认删除组织架构编号为"' + organizationalIds + '"的数据项？').then(function() {
        return delMemberOrganizational(organizationalIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('api/memberOrganizational/export', {
        ...this.queryParams
      }, `memberOrganizational_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
